package com.example.controller;


import com.example.entity.Account2;
import com.example.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.util.Result;

@RestController
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountService accountService;

    @PostMapping("/register")
    public Result<String> register(@RequestBody Account2 account) {
        boolean success = accountService.register(account);
        return success ? Result.success("注册成功") : Result.fail("账号已存在");
    }

    @PostMapping("/login")
    public Result<Account2> login(@RequestBody Account2 loginRequest) {
        Account2 account = accountService.login(loginRequest.getAccountnumber(), loginRequest.getPassword());
        return account != null ? Result.success("登录成功", account) : Result.fail("账号或密码错误");
    }

    @PostMapping("/updateInfo")
    public Result<String> updateInfo(@RequestBody Account2 account) {
        return accountService.updateAccount(account)
                ? Result.success("更新成功")
                : Result.fail("更新失败");
    }

    @PostMapping("/updaterole")
    public Result<String> updateRole(@RequestParam String accountnumber) {
        return accountService.updateRole(accountnumber)
                ? Result.success("角色更新成功")
                : Result.fail("角色更新失败");
    }

}
